package com.avyss.flyingpoints.scene;

public class Follower extends AbstractFlyingObject {

	public Follower(DynamicsParams params) {
		super(params);
	}

	public void adjustVelociyFollower(AbstractFlyingObject leader, long timeStepMs) {
	    float   leaderDx;
	    float   leaderDy;
	    float   leaderDistance;
	    float   velocityIncrX;
	    float   velocityIncrY;
	    
        // Calculate acceleration (in direction of the leader)
        leaderDx = posX - leader.posX;
        leaderDy = posY - leader.posY;
        leaderDistance = (float) Math.sqrt(leaderDx * leaderDx + leaderDy * leaderDy);
        if (leaderDistance < params.getLeaderNonattractingDistance()) {
            return;
        }
        
        float velocityIncrTotal = timeStepMs * params.getMaxVelocity() * params.getLeaderAttractionCoeff();
        velocityIncrX = (- leaderDx / leaderDistance) * velocityIncrTotal;
        velocityIncrY = (- leaderDy / leaderDistance) * velocityIncrTotal;

        // Apply the acceleration
        velX += velocityIncrX;
        velY += velocityIncrY;

        // Don't permit too big velocity
        if ( velX > params.getMaxVelocity() ) {
             velX = params.getMaxVelocity();
        }
        if ( velY > params.getMaxVelocity() ) {
             velY = params.getMaxVelocity();
        }
	}
	
}
